﻿package
{
	import flash.display.MovieClip;
	import flash.events.*;
	import bullet;
	
	public class Ship extends MovieClip
	{
		public var accel:Number = 2;
		public var friction:Number = 0.9;
		public var topSpeed:Number = 6;
		public var vx:Number = 0;
		public var vy:Number = 0;
		
		public function Ship( x:Number, y:Number )
		{
			this.x = x;
			this.y = y;
			//addEventListener( Event.ENTER_FRAME, update, false, 0, true );
		}
		
		public function fire() : bullet
		{
			return new bullet(x + width/2, y);
		}
		
		public function update( up:Boolean, down:Boolean, left:Boolean, right:Boolean, space:Boolean ) : void
		{
			var tempX:Number = x;
			var tempY:Number = y;
			
			if( up )
			{
				vy -= accel;
				if( -vy > topSpeed )
					vy = -topSpeed;
			}
			else if( down )
			{
				vy += accel;
				if( vy > topSpeed )
					vy = topSpeed;
			}
			else
			{
				vy *= friction;
			}
			
			if( left )
			{
				vx -= accel;
				if( -vx > topSpeed )
					vx = -topSpeed;
			}
			else if( right )
			{
				vx += accel;
				if( vx > topSpeed )
					vx = topSpeed;
			}
			else
			{
				vx *= friction;
			}
			
			tempX += vx;
			tempY += vy;
			x = tempX;
			y = tempY;
		}
					
	}
}